package com.mjlf.t.t1.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @ClassName Server
 * @auther mjlf
 * @Date 2019/9/17 10:00
 * @Description TODO
 */
public class Server {

    public static void main(String[] args) {
        //accept 处理组，接收客户端连接的线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        //工作组，真正处理读写事件的线程组
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try{
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workGroup)
                    //确定服务端用什么通道，这里使用Nio,可以使用Bio，还可以使用其他
                    .channel(NioServerSocketChannel.class)
                    //处理已经连接上的客户端
                    .childHandler(new TestServerInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(8989).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }

    }
}
